package com.hc.javabasestudyproject.feature.stream;

import com.hc.javabasestudyproject.feature.entity.Author;
import com.hc.javabasestudyproject.feature.entity.DataUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author: huangcheng
 * @CreateTime: 2023-11-22  17:17
 * @Description:
 * @Version: 1.0
 */
public class CollectStream {
    public static void main(String[] args) {
        List<Author> dataList = DataUtils.getAuthors();
        //直接获取最大值
        dataList.stream()
                .map(Author::getAge)
                .max(Comparator.comparingInt(Integer::valueOf))
                .ifPresent(s-> System.out.println(s));
        //先收集，在获取最大值
        dataList.stream()
                .map(Author :: getAge)
                .collect(Collectors.maxBy(Integer::compareTo))
                .ifPresent(s -> System.out.println(s));
        //获取年龄总和
        Double sumAge = dataList.stream()
                .collect(Collectors.summingDouble(Author::getAge));
        System.out.println(sumAge);
        //多重分组，先根据id，在根据age
        Map<Long, Map<Integer, List<Author>>> collect = dataList.stream()
                .collect(Collectors.groupingBy(Author::getId, Collectors.groupingBy(Author::getAge)));
        System.out.println(collect);
        //分区
        Map<Boolean, List<Author>> collect1 = dataList.stream()
                .collect(Collectors.partitioningBy(author -> author.getAge() > 20));

        Map<Long, Author> collect2 = dataList.stream().
                collect(Collectors.groupingBy(Author::getId,Collectors.collectingAndThen(
                        Collectors.maxBy(Comparator.comparing(Author::getAge)),author -> author.get())));
        System.out.println(collect2);
    }
}
